package com.jxnu.escms.Control.InOutControl;

import com.jxnu.escms.Control.UserControl.UserControl;
import com.jxnu.escms.Pojo.InOutRecord;
import com.jxnu.escms.Pojo.PageResult;
import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.InOutService.InOutService;
import com.jxnu.escms.Service.UserService.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
public class InOutControl {

    @Resource
    private InOutService inOutService;

    @Resource
    private UserService userService;

    @Resource
    private UserControl userControl;

    @RequestMapping("/apply")
    public Map<String, String> applyInOut(@RequestBody Map<String, Object> data) throws ParseException {
//        {id=2,name=张三, phone=123456, inOut=出, time=[2022-10-17T16:00:00.000Z, 2022-10-19T15:59:59.000Z],
//        reason=ssss, healthCode=绿码, temperature=36.5, address=昌九社区10栋西单元331门牌号, remark=, approver=1}

        List<String> time = (List<String>) data.get("time");
        data.remove("time");

        Map<String, Object> d = new HashMap<>();

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String outTime = df.format(format1.parse(time.get(0).replace("Z", " UTC")));
        String returnTime = df.format(format1.parse(time.get(1).replace("Z", " UTC"))) ;

        // 添加进出时间
        d.put("outTime", outTime);
        d.put("returnTime", returnTime);

        // 增加申请时间
        d.put("appTime", df.format(new Date()));

        d.put("id", data.get("id"));
        d.put("inOut",data.get("inOut"));
        d.put("reason", data.get("reason"));
        d.put("admin", data.get("approver"));

        // 添加申请记录
        int i = inOutService.addApply(d);
        Map<String, String> r = new HashMap<>();
        r.put("apply", i == 1? "success":"worry");
        return r;
    }

    @RequestMapping("/record")
    public Map<String, Object> getRecord(@RequestBody Map<String, Integer> data){

        Integer start = data.get("start");
        Integer pageSize = data.get("pageSize");
        Integer userId = data.get("userId");

        PageResult<InOutRecord> records = inOutService.queryRecord(userId, start, pageSize);

        System.out.println(records);

        Map<String, Object> res = new HashMap<>();
        res.put("res", records);
        return res;
    }

    @RequestMapping("/deleterecord")
    public Map<String, String> deleteRecord(@RequestBody Map<String, Object> data){
        List<String> time = (List<String>) data.get("time");
        Integer userId = (Integer) data.get("userId");

        if(time.size() != 0){
            time.forEach(p->{
                inOutService.deleteRecord(userId, p);
            });
        }

        Map<String, String> r = new HashMap<>();
        r.put("delete", "success");
        return r;
    }

    @RequestMapping("/getRecords")
    public Map<String, Object> getRecords(@RequestBody Map<String, Integer> data){
        Integer start = data.get("start");
        Integer pageSize = data.get("pageSize");
        Integer adminId = data.get("adminId");
        Integer kill = data.get("kill");

        PageResult<InOutRecord> records = inOutService.getRecords(adminId, start, pageSize, kill);
        for(InOutRecord i : records.getPages()){
            int userId = i.getApplicantId();
            User user = userService.getUserByID(userId);
            i.setName(user.getRealName());
            i.setHealthCode(user.getHealthCode() + "码");
            i.setAddress(userControl.getAddress(user, user.getId()));
        }

        Map<String, Object> res = new HashMap<>();
        res.put("res", records);
        return res;
    }

    @RequestMapping("/audit")
    public Map<String, Object> auditRecord(@RequestBody Map<String, Object> data){
        int id = (int) data.get("id");
        String result = (String) data.get("result");

        int i = inOutService.auditRecord(id, result);

        Map<String, Object> res = new HashMap<>();
        res.put("res", i == 1? "success":"worry");
        return res;
    }

    @RequestMapping("/addrecord")
    public Map<String, String> addRecord(@RequestBody Map<String, Object> data) {
        List<Integer> recordId = (List<Integer>) data.get("recordId");
        Integer adminId = (Integer) data.get("adminId");
        String kill = (String) data.get("kill");

        if(recordId.size() != 0){
            recordId.forEach(p->{
                inOutService.addControlRecord(adminId, p, kill);
            });
        }
        Map<String, String> r = new HashMap<>();
        r.put("operation", "success");
        return r;
    }

    @RequestMapping("/filter")
    public Map<String, Object> filterRecord(@RequestBody Map<String, Object> data){
        String name = (String) data.get("name");
        String build = (String) data.get("build");
        String nuit = (String) data.get("nuit");
        Integer adminId = (Integer) data.get("adminId");
        int kill = (int) data.get("kill");

        List<InOutRecord> res1 = new ArrayList<>();
        List<InOutRecord> res2 = new ArrayList<>();
        List<InOutRecord> res3 = new ArrayList<>();

        List<InOutRecord> records = inOutService.filterRecords(adminId, kill);
        for(InOutRecord r: records){
            int userId = r.getApplicantId();
            User user = userService.getUserByID(userId);
            r.setName(user.getRealName());
            r.setHealthCode(user.getHealthCode() + "码");
            String address = userControl.getAddress(user, user.getId());
            r.setAddress(address);

            if(Objects.equals(name, "")){
                res1.add(r);
            }
            else if(Objects.equals(name, r.getName())){
                res1.add(r);
            }
        }

        for(InOutRecord i :res1){
            String b = (String) userControl.parseAddress(i.getAddress()).get(1);
            if(!(!Objects.equals(build, "") && !Objects.equals(build, b))){
                res2.add(i);
            }
        }

        for(InOutRecord i :res2){
            String n = (String) userControl.parseAddress(i.getAddress()).get(2);
            if(!(!Objects.equals(nuit, "") && !Objects.equals(nuit, n))){
                res3.add(i);
            }
        }

        Map<String, Object> r = new HashMap<>();
        r.put("data", res3);
        r.put("total", res3.size());
        return r;
    }

    @RequestMapping("/clear")
    public Map<String, String> clearAll(@RequestBody Map<String, Object> data){
        List<Integer> ids = (List<Integer>) data.get("ids");
        if(ids.size() != 0){
            ids.forEach(p->{
                inOutService.deleteById(p);
            });
        }
        Map<String, String> res = new HashMap<>();
        res.put("delete", "success");
        return res;
    }
}
